package com.person.ntv.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.person.easyui.EasyUItools;
import com.person.easyui.TreeNode;
import com.person.easyui.TreeNodeRule;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysRole;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysDeptService;
import com.person.ntv.service.sys.SysOrgService;
import com.person.ntv.service.sys.SysResourceService;
import com.person.ntv.service.sys.SysRoleService;
import com.person.ntv.service.sys.SysUserService;

@Controller
@RequestMapping("/system/sysPermission")
public class SysPermissionController {
	@Autowired
		private SysRoleService  sysRoleService ;
	@Autowired
		private SysResourceService  sysResourceService ;
	@Autowired
		private CommonService commonService ;
	@Autowired
	    private SysOrgService sysOrgService;
	@Autowired
	    private SysDeptService sysDeptService;
	@Autowired
	    private SysUserService  sysUserService;
	
		@RequestMapping("/listPage")
		public String  getCodePage(ModelMap mv)throws Exception{
			return "/system/permission/permissionList";
		}
		
		@RequestMapping("/orgList")
		@ResponseBody
		public List<TreeNode> getOrgList(String roleId)throws Exception{
			List<Map<String,Object>> list = sysOrgService.getOrgDeptUser(roleId);
			List<TreeNode> nodeList=EasyUItools.getUItreeList(EasyUItools.getTreeNodeListByMap(list,
					new TreeNodeRule("id","parentId","treeName","state","checked","icon")));
			return nodeList;
		}
		
		@RequestMapping("/menuList")
		@ResponseBody
		public List<TreeNode> getMenuList(String roleId) throws Exception{
			List<Map<String,Object>>list=sysResourceService.getMenuTreeByRole(roleId);
			for(Map<String,Object> map:list){
				 if(map.get("checked")!=null&&"1".equals(map.get("checked").toString()))
					  map.put("checked","true");
				  else 
					 map.put("checked","false");
				} 
			List<TreeNode> nodeList=EasyUItools.getUItreeList(EasyUItools.getTreeNodeListByMap(list,
					new TreeNodeRule("id","parentId","resName","state","checked","icon")));
			 for(TreeNode t:nodeList)
				 nodeChecked(t);
			 return nodeList;
		}	
		
		private void nodeChecked(TreeNode treeNode){
			if(treeNode==null) return;
			if(treeNode.getChildren()!=null&&treeNode.getChildren().size()>0){
				treeNode.setChecked(null);
				 for(TreeNode tn:treeNode.getChildren())
					 nodeChecked(tn);
			}
		}
		
		
		
		@RequestMapping("/pageList")
		@ResponseBody
		public List<TreeNode> getPageList(String roleId) throws Exception{
			List<Map<String,Object>>list=sysResourceService.getPageTreeByRole(roleId);
			
			for(Map<String,Object> map:list){
				 if(map.get("checked")!=null&&"1".equals(map.get("checked").toString()))
					 map.put("checked","true");
				 else 
					 map.put("checked","false");
				 String resName=String.valueOf(map.get("resName"));
				 String actionUrl=map.get("actionUrl")!=null&&StringUtils.isNotBlank(map.get("actionUrl").toString())?"("+map.get("resCode").toString()+":"+String.valueOf(map.get("actionUrl"))+")":"("+map.get("resCode").toString()+")";
				 map.put("resName", resName+actionUrl);
				} 
			List<TreeNode> nodeList= EasyUItools.getUItreeList(EasyUItools.getTreeNodeListByMap(list,
					new TreeNodeRule("id","parentId","resName","state","checked",null)));
			 for(TreeNode t:nodeList)
				 nodeChecked(t);
			return nodeList;
		}
		
		@RequestMapping("/roleList")
		@ResponseBody
		public List<Map<String,Object>> getRoleList(String roleName) throws Exception{
		    List<SysRole> roleList=sysRoleService.selectAll();
		    List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
		    if(roleList!=null)
		    	for(SysRole role:roleList){
		    		Map<String,Object> map=new HashMap<String,Object>();
					map.put("id", role.getId());
					map.put("value",role.getId());
					map.put("text",role.getRoleName()+"("+role.getRoleCode()+")");
					if((role.getRoleName()!=null&&role.getRoleName().indexOf(roleName)!=-1)||
							(role.getRoleCode()!=null&&role.getRoleCode().indexOf(roleName)!=-1))
					list.add(map);
		    	}
		    return list;
		}
		
	 	
		@RequestMapping("/updatePermission")
		@ResponseBody
		public Map<String,Object>  updatePermission(ModelMap mv,String []ids,String roleId,String type) throws Exception{
			Map<String,Object> map=new HashMap<String,Object>();
			try {
				sysResourceService.updateProvision(ids, roleId,type);
				map.put("error", "0");
				map.put("msg","分配成功！");
			} catch (ServiceException e) {
				map.put("error", "1");
				map.put("msg",e.getMessage()+"分配失败！");
			}
			return map;
		}
		
}
